import time
from selenium.common.exceptions import NoSuchElementException
import os.path
from selenium.webdriver.common.by import By

from selenium.webdriver.support.wait import WebDriverWait

from framework.logger import Logger
from framework.getyaml import getYaml

logger = Logger(logger='BasePage').getlog()

class BasePage(object):
    def __init__(self,driver):
        self.driver = driver

    def open(self,url):
        return self.driver.get(url)

    def quit_browser(self):
        self.driver.quit()

    #浏览器前进操作
    def forward(self):
        self.driver.forward()

    #浏览器后退操作
    def back(self):
        self.driver.back()

    #显示等待（直到某个元素出现）
    def wait(self,loc,seconds):
        try:
            wait_=WebDriverWait(self.driver,seconds)
            wait_.until(lambda driver:driver.find_element(*loc))
        except NameError as e:
            logger.error("Failed to load the element with %s" % e)

    #保存图片
    def get_windows_img(self):
        #保存到项目根目录的文件夹.\Screenshots下
        file_path = os.path.dirname(os.path.dirname(__file__)) + '/screenshots/'
        rq = time.strftime('%Y%m%d%H%M',time.localtime(time.time()))
        screen_name = file_path + rq + '.png'
        try:
            self.driver.get_screenshot_as_file(screen_name)
        except NameError as e:
            logger.error('failed to take screenshot! %s' %e)
            self.get_windows_img()


    #获取元素
    def find_element(self,selector):
        selector_by = selector.split('=>')[0]
        selector_value = selector.split('=>')[1]
        if selector_by == 'id':
            element = self.driver.find_element_by_id(selector_value)
        elif selector_by == 'name':
            element = self.driver.find_element_by_name(selector_value)
        elif selector_by == 'css':
            element = self.driver.find_element_by_css_selector(selector_value)
        elif selector_by == 'class':
            element = self.driver.find_element_by_class_name(selector_value)
        elif selector_by =='xpath':
            element = self.driver.find_element_by_xpath(selector_value)
        elif selector_by == 'tag_name':
            element = self.driver.find_element_by_tag_name(selector_value)
        elif selector_by =='link_text':
            element = self.driver.find_element_by_link_text(selector_value)
        elif selector_by =='partial_link_text':
            element = self.driver.find_element_by_partial_link_text(selector_value)
        else:
            raise NameError("无法定位到的元素类型.")
        return element


    #获取元素
    def find_elements(self,selector):
        selector_by = selector.split('=>')[0]
        selector_value = selector.split('=>')[1]
        if selector_by == 'id':
            elements = self.driver.find_elements_by_id(selector_value)
        elif selector_by == 'name':
            elements = self.driver.find_elements_by_name(selector_value)
        elif selector_by == 'css':
            elements = self.driver.find_elements_by_css_selector(selector_value)
        elif selector_by == 'class':
            elements = self.driver.find_elements_by_class_name(selector_value)
        elif selector_by =='xpath':
            elements = self.driver.find_elements_by_xpath(selector_value)
        elif selector_by == 'tag_name':
            elements = self.driver.find_elements_by_tag_name(selector_value)
        elif selector_by =='link_text':
            elements = self.driver.find_elements_by_link_text(selector_value)
        elif selector_by =='partial_link_text':
            elements = self.driver.find_elements_by_partial_link_text(selector_value)
        else:
            raise NameError("无法定位到的元素类型.")
        return elements

    #输入
    def send_keys(self,selector,text):
        element = self.find_element(selector)
        element.clear()
        try:
            element.send_keys(text)
        except NameError as e:
            logger.error("Failed to send keys with %s" % e)

    #清除文本框
    def clear(self,selector):
        element = self.find_element(selector)
        try:
            element.clear()
        except NameError as e:
            logger.error('failed to clear with %s' % e)

    #点击元素
    def click(self,selector):
        element = self.find_element(selector)
        try:
            element.click()
        except NameError as e:
            logger.error('failed to click with %s' % e)

    #获取元素的text
    def get_text(self,selector):
        return self.find_element(selector).text









